package com.leo.foodmaster.admin.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.entity.Organ;
import com.leo.foodmaster.admin.system.service.IOrganService;
import com.leo.foodmaster.commons.annotation.Inner;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.entity.TreeSelect;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 机构表 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-06-23
 */
@Api(tags = "机构信息管理")
@RestController
@AllArgsConstructor
@RequestMapping("/system/organ")
public class OrganController {
    private final IOrganService organService;


    @Inner
    @ApiOperation("查询公司树形结构")
    @GetMapping("/treeSelect")
    public Result<List<TreeSelect>> treeSelect() {
        Long organId = WebUtils.getUser().getOrganId();
        List<TreeSelect> treeSelect = organService.treeSelect(organId);
        return Result.success(treeSelect);
    }

    @ApiOperation("条件分页查询公司树形结构")
    @GetMapping("/treeList")
    public Result<IPage<Organ>> treeList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "公司名称") @RequestParam(required = false) String name,
            @ApiParam(value = "公司状态") @RequestParam(required = false) String status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime ) {
        IPage<Organ> treeList  = organService.treeList(new Page<>(pageNum, pageSize), name,
                status, beginTime, endTime);
        return Result.success(treeList);
    }

    @ApiOperation("条件分页查询公司列表")
    @GetMapping("/list")
    public Result<IPage<Organ>> getOrganPageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "公司名称") @RequestParam(required = false) String name,
            @ApiParam(value = "公司状态") @RequestParam(required = false) String status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime ) {
        IPage<Organ> pageList  = organService.getOrganPageList(new Page<>(pageNum, pageSize), name,
                status, beginTime, endTime);
        return Result.success(pageList);
    }

    @PostMapping
    @ApiOperation("新增公司")
    public Result<Object> addOrgan(@RequestBody Organ organ) {
        return organService.addOrgan(organ);
    }

    @PutMapping
    @ApiOperation("修改公司")
    public Result<Object> updateOrgan(@RequestBody Organ organ) {
        return organService.updateOrgan(organ);
    }

    @PutMapping("/changeStatus")
    @ApiOperation("修改公司状态")
    public Result<Object> changeOrganStatus(@RequestBody Organ organ) {
        return organService.changeOrganStatus(organ);
    }

    @DeleteMapping("/{organIds}")
    @ApiOperation("删除公司")
    public Result<Object> deleteOrgan(@ApiParam("公司Ids") @PathVariable String organIds) {
        return organService.deleteOrgan(organIds);
    }

}

